package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.FXRModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 佟媛媛 on 18/10/23.
 */

@Component("I1012")
@Scope("prototype")
public class I1012 extends BaseComponent {

    private static Logger log = LoggerFactory.getLogger(I1012.class);


    /**
     * @auth 佟媛媛
     * @date 18/10/29 下午7:13
     * @desc    lssws   sjsws
     * 检查企业、主承销商及其他承销机构、律师事务所、会计师事务所、信用增进机构、信用评级机构、登记托管、结算机构以及其他与发行有关机构等是否披露名称、住所、法定代表人、联系电话、传真和联系人姓名信息。
     * 若某一项未披露，提示“XX文件XX信息未披露”
     * "1、检查机构包括：检查企业、主承销商及其他承销机构、律师事务所、会计师事务所、信用增进机构、信用评级机构、登记托管、结算机构、集中簿记建档系统技术支持机构；
    2、检查发行机构对应的基本信息：名称、住所、法定代表人、联系电话、传真和联系人姓名；
    3、上述检查项如果不存在，提示缺失；
    4、提示未检查其他发行结构，需人工审核；"
     */
    @Override
    public List<ComponentResult> handlerM() {
        return common("M-12");
    }

    private String getKeyword(String key){
        Map<String,String> items = new HashMap();
        items.put("DH", "联系电话");
        items.put("CZ", "传真");
        items.put("LXR", "联系人姓名");
        items.put("FDDBR", "法定代表人");
        items.put("MC", "名称");
        items.put("FXRMC", "名称");
        items.put("LXDZ", "住所");
        return items.get(key);
    }

    private List<ComponentResult> common(String catalogId){
        List<ComponentResult> list = new ArrayList<>();
        setCatalogId(catalogId);
        Map modelMap = new HashMap();
        modelMap.put("FXR","发行人");
        modelMap.put("ZCXS","主承销商");
        modelMap.put("LSSWS","律师事务所");
        modelMap.put("SJSWS","会计事务所");
        modelMap.put("XYZJJG","信用增进机构");
        modelMap.put("XYPJJG","信用评级机构");
        modelMap.put("DJTG","登记托管");
        modelMap.put("JZBJJDJG","集中簿记建档系统技术支持机构");
        String[] models = new String[]{"FXR","ZCXS","LSSWS","SJSWS","XYZJJG","XYPJJG","DJTG","JZBJJDJG"};
        String[] values = new String[]{"FXRMC","LXDZ","FDDBR","DH","CZ","LXR"};
        String[] values2 = new String[]{"MC","LXDZ","FDDBR","DH","CZ","LXR"};
        for(String model : models){
            if("FXR".equals(model)){
                for(String v : values){
                    KVText value = getKVTextKey(getCatalogId(),model,v);
                    list.add(getComponentResult(value,null,(String) modelMap.get(model)+"-"+getKeyword(v),(String) modelMap.get(model),"缺失"));
                }
            }else{
                for(String v : values2){
                    KVText value = getKVTextKey(getCatalogId(),model,v);
                    list.add(getComponentResult(value,null,(String) modelMap.get(model)+"-"+getKeyword(v),(String) modelMap.get(model),"缺失"));
                }
            }
        }
        return list;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return common("BM-12");
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return common("DM-12");
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return common("BDM-12");
    }

    private ComponentResult getComponentResult(KVText value,String text,String keyword,String ruleName,String promptMsg){
        String exist = "02";
        int pageNum = getChapterModel(getCatalogId()).getStartPage();
        boolean annotationFlag = true;
        List<ComponentResult.TextPosition> textPositionList = null;
        try{
            if(value!=null){
                annotationFlag = false;
                pageNum = value.getPageNumber();
                exist = "01";
                textPositionList = getContentAndPositionList(value);
            }
            return getComponentResult(annotationFlag,exist,keyword,pageNum,promptMsg,ruleName,textPositionList);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return getComponentResult(annotationFlag,exist,keyword,pageNum,promptMsg,ruleName,textPositionList);
        }
    }


    private ComponentResult getComponentResult(boolean annotationFlag,String exist,String keyword,int pageNum,String promptMsg,String ruleName,List<ComponentResult.TextPosition> textPositionList){
        ComponentResult result = new ComponentResult();
        result.setAnnotationFlag(annotationFlag);
        result.setCatalogId(getCatalogId());
        result.setChapterId(getChapterId());
        result.setExist(exist);
        result.setKeyword(keyword);
        result.setPageNum(pageNum);
        result.setPromptMessage("01".equals(exist)?"正确":promptMsg);
        result.setRegulationId(getRegulationId());
        result.setRuleName(ruleName);
        result.setTextPosition(textPositionList);
        result.setUid(UUID.randomUUID().toString());
        return result;
    }

}
